--[[
 -- *名称:PairTypeAnalyzer
 -- *描述:对子
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-10
 -- *修改日期:2017-11-10
 ]]
local DdzSuitAnalyzer = require(basePath.."suit/ddzsuitanalyzer");
local PairTypeAnalyzer = pokercore.class(DdzSuitAnalyzer,false);

local TUPLE_SIZE = 2 ;

function PairTypeAnalyzer:getSuitType()
	return DdzCardTypes.PAIR;
end

--@Override
function PairTypeAnalyzer:doCheckRule( cards, term, result )
	if (result:getCardNum() == TUPLE_SIZE) then
		if (result:getLaziCardNum() == 0 or result:isAllCardLazi()) then -- // 没有癞子，都是癞子
			--print("PairTypeAnalyzer:doCheckRule 1");
			return #result:getAllValueCounter():getValuesInCount(2) == 1;
		else --// 一张癞子的情况
			if (result:getNoneLaziValueCounter():hasJoker()) then --// 非赖中含有大小王
				--print("PairTypeAnalyzer:doCheckRule 2");
				return false;
			else 
				return true;
			end
		end
	else 
		--print("PairTypeAnalyzer:doCheckRule 3");
		return false;
	end
end


--@Override
function PairTypeAnalyzer:doAnalysizeLaizi(cards,term,onlyMaxValue)
	local cardsResult = {}
	local result = cards:getResult();
	if (result:getLaziCardNum() == 1) then
		local laiziCardList = cards:subList(result:getCardNum() - result:getLaziCardNum() + 1, result:getCardNum());
		local value = result:getNoneLaziValueCounter():getAllValues()[1];
		for _,card in ipairs(laiziCardList) do -- // 设置number
			card:changeCard(value);
		end
		local cardsCopy = cards:clone();
		self:analysize(cardsCopy, term);
		table.insert(cardsResult,cardsCopy:clone());
	else 
		for _,card in ipairs(cards) do -- // 设置number
			card:changeCard(card:getNumber());
		end
		local cardsCopy = cards:clone();
		self:analysize(cardsCopy, term);
		table.insert(cardsResult,cardsCopy:clone());
	end
	return cardsResult;
end

--@Override
function PairTypeAnalyzer:containsPossibleCards(handCards,term) 
	local result = handCards:getResult();
	return result:getCardNum() - result:getJokerCardNum() >= TUPLE_SIZE;
end

--/** 最多使用癞子的数量 */
local MAX_USE_LAIZI_CARD_NUM = 2;
	
--@Override
function PairTypeAnalyzer:doListPossibleCards(handCards,term) 
	local cardsResult = {};
	--// 非癞子部分
	local result = handCards:getResult();
	local noneLaiziCardList = handCards:subList(1, result:getNoneCardNum());
	local noneLaiziCards = DdzTypeUtil.createCardsCopy(noneLaiziCardList);
	noneLaiziCards:setResult(handCards:getResult():clone());
	--// 癞子部分
 	local laiziCardList = handCards:subList(result:getNoneCardNum()+1, result:getCardNum());
	local laiziCards = DdzTypeUtil.createCardsCopy(laiziCardList);
	laiziCards:setResult(handCards:getResult():clone());

	local laiziCardNum = result:getLaziCardNum();
	local maxUseLaiziCardNum = MAX_USE_LAIZI_CARD_NUM < laiziCardNum and MAX_USE_LAIZI_CARD_NUM or laiziCardNum;
	for useLaiziCardNum = 0,maxUseLaiziCardNum do
		local needCardNum = TUPLE_SIZE - useLaiziCardNum;
		if (needCardNum > 0) then -- // 部分用癞子的情况
			local useLaiziCardList = DdzTypeUtil.cloneSublistedCards(laiziCards, 1, useLaiziCardNum);--// 补癞子
			local cardsList = {} ;
			if (needCardNum == 1) then
				table.addAll(cardsList,DdzTypeUtil.selectSingleCards(noneLaiziCards, false));
			else 
			    table.addAll(cardsList,DdzTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum));
			end
			for _,usecards in ipairs(cardsList) do
				local card = usecards:get(1);
				for _,laiziCard in ipairs(useLaiziCardList) do
					laiziCard:changeCard(card:getValue());
				end
				local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
				if (self:match(cardsCopy, term)) then
					self:analysize(cardsCopy, term);
					table.insert(cardsResult,cardsCopy:clone());
				end
			end
		else -- // 全部用癞子的情况
			local useLaiziCardList = DdzTypeUtil.selectTupleCountCards(laiziCards, TUPLE_SIZE);
			for _,useLaiziCards in ipairs(useLaiziCardList) do
				for _, card in ipairs(useLaiziCards) do
					card:changeCard(card:getNumber());
				end
				local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(useLaiziCards, nil);
				if (self:match(cardsCopy, term)) then
					self:analysize(cardsCopy, term);
					table.insert(cardsResult,cardsCopy:clone());
				end
			end
		end
	end
	--dump(cardsResult,"cardsResult");
	return cardsResult;
end

return PairTypeAnalyzer;

